home *** CD-ROM | disk | FTP | other *** search
/ 100 Great Games for Palm OS 2 / PalmV2012301.ISO / puzzles / Pilot Mines / src / hscore.c < prev    next >
C/C++ Source or Header  |  1999-06-01  |  5KB  |  209 lines

  1. /*
  2.  * PilotMines is Copyright (c) 1997-1999 by Thomas Pundt
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software for any
  5.  * purpose, without fee, and without a written agreement is hereby granted,
  6.  * provided that the above copyright notice and this paragraph and the
  7.  * following two paragraphs appear in all copies.
  8.  *
  9.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
  10.  * SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
  11.  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
  12.  * THE AUTHOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  13.  *
  14.  * THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
  15.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  16.  * PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"
  17.  * BASIS, AND THE AUTHOR HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT,
  18.  * UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  19.  *
  20.  */
  21.  
  22. #include <Common.h>
  23. #include <System/KeyMgr.h>
  24. #include <System/SysAll.h>
  25. #include <UI/UIAll.h>
  26.  
  27. #include "minercp.h"
  28. #include "mine.h"
  29. #include "hscore.h"
  30.  
  31. int insertPos;
  32.  
  33. static VoidPtr Id2Ptr(Word id)
  34. {
  35.   FormPtr frm = FrmGetActiveForm();
  36.   return FrmGetObjectPtr(frm, FrmGetObjectIndex(frm,id));
  37. }
  38.  
  39. void InitHighScore()
  40. {
  41.   int i,j;
  42.   for (i=0; i<3; i++)
  43.     for (j=0; j<4; j++) {
  44.       game.hscore[i][j].score   = 0;
  45.       game.hscore[i][j].name[0] = 0;
  46.       game.hscore[i][j].date    = 0;
  47.     }
  48. }
  49.  
  50. Boolean isHighScore(int score)
  51. {
  52.   int i;
  53.  
  54.   for (i=0; i<5; i++) {
  55.     if (game.hscore[game.level][i].score != 0 
  56.         && game.hscore[game.level][i].score <= score)
  57.       continue;
  58.     return true;
  59.   }
  60.   return false;
  61. }
  62.  
  63. static void DrawScoreAt(int score, ULong when, SWord x, SWord y)
  64. {
  65.   DateTimeType date;
  66.   SystemPreferencesType sysPrefs; 
  67.   char buf[20];
  68.  
  69.   if (!score)
  70.     return;
  71.  
  72.   StrCopy(buf,"00:00");
  73.   
  74.   if (score>3559)
  75.     score = 3559;
  76.   buf[4] = score % 10 + 48;
  77.   buf[3] = (score / 10) % 6 + 48;
  78.   buf[1] = (score / 60) % 10 + 48;
  79.   buf[0] = (score / 600) % 10 + 48;
  80.   WinDrawChars(buf, 5, x, y);
  81.  
  82.   TimSecondsToDateTime(when, &date);
  83.   PrefGetPreferences (&sysPrefs);
  84.   DateToAscii(date.month, date.day, date.year, sysPrefs.dateFormat, buf);
  85.   WinDrawChars(buf, StrLen(buf), x+35, y);
  86.  
  87. }
  88.  
  89. static void InsertHighScore(int score)
  90. {
  91.   int j;
  92.   VoidHand handle;
  93.   CharPtr name;
  94.   FieldPtr fld;
  95.   FormPtr frm;
  96.   FontID tmp;
  97.   char buf[8];
  98.  
  99.   StrCopy (buf, "Level 0");
  100.   insertPos = 5;
  101.  
  102.   tmp = FntSetFont(boldFont);
  103.  
  104.   if (score) {
  105.     for (insertPos=0; insertPos<5; insertPos++) {
  106.       if (game.hscore[game.level][insertPos].score != 0 
  107.           && game.hscore[game.level][insertPos].score <= score)
  108.         continue;
  109.       for (j=3; j>=insertPos; j--) {
  110.         game.hscore[game.level][j+1].score = game.hscore[game.level][j].score;
  111.         game.hscore[game.level][j+1].date  = game.hscore[game.level][j].date;
  112.         game.hscore[game.level][j].name[10] = 0;
  113.         StrCopy(game.hscore[game.level][j+1].name, 
  114.                 game.hscore[game.level][j].name);
  115.       }
  116.       game.hscore[game.level][insertPos].score = score;
  117.       game.hscore[game.level][insertPos].date  = TimGetSeconds();
  118.  
  119.       fld = Id2Ptr(ID_HighScoreField+insertPos);
  120.  
  121.       if (!(handle = (VoidHand)FldGetTextHandle(fld))) {
  122.         handle = MemHandleNew(NAMLEN*sizeof(char));
  123.         name = MemHandleLock(handle);
  124.         name[0] = '\0';
  125.         MemHandleUnlock(handle);
  126.         FldSetTextHandle(fld, (Handle)handle);
  127.       } else {
  128.         name = MemHandleLock(handle);
  129.         name[0] = '\0';
  130.         MemHandleUnlock(handle);
  131.       }
  132.  
  133.       FldSetUsable(fld, true);
  134.       FldDrawField(fld);
  135.       frm = FrmGetActiveForm();
  136.       FrmSetFocus(frm, FrmGetObjectIndex(frm, ID_HighScoreField+insertPos));
  137.  
  138.       break;
  139.     }
  140.   }
  141.  
  142.   buf[6] = game.level+49;
  143.   WinDrawChars(buf, 7, 60, 15);
  144.  
  145.   for (j=0; j<5; j++) {
  146.     if (j!=insertPos) {
  147.       WinDrawChars(game.hscore[game.level][j].name, 
  148.                    StrLen(game.hscore[game.level][j].name), 5, 45+j*15 );
  149.     }
  150.     DrawScoreAt(game.hscore[game.level][j].score,
  151.                 game.hscore[game.level][j].date, 75,45+j*15);
  152.   }
  153.  
  154.   FntSetFont(tmp);  
  155.  
  156. }
  157.  
  158. static void CloseHighScore()
  159. {
  160.   FieldPtr fld;
  161.   CharPtr  name;
  162.  
  163.   if (insertPos<5) {
  164.     fld = Id2Ptr(ID_HighScoreField+insertPos);
  165.     name = FldGetTextPtr(fld);
  166.     StrCopy(game.hscore[game.level][insertPos].name, name ? name:"");
  167.     FldSetUsable(fld, false);
  168.   }
  169. }
  170.  
  171. Boolean HighscoresFormHandleEvent(EventPtr e)
  172. {
  173.   Boolean handled;
  174.   
  175.   CALLBACK_PROLOGUE
  176.   handled = false;
  177.   
  178.   switch (e->eType) {
  179.  
  180.   case frmOpenEvent:
  181.     FrmDrawForm(FrmGetActiveForm());
  182.     InsertHighScore(score);
  183.     handled = true;
  184.     break;
  185.  
  186.   case ctlSelectEvent:
  187.     if (e->data.ctlSelect.controlID == ID_HighScoreDone) {
  188.       CloseHighScore();
  189.       FrmReturnToForm(0);
  190. // Insert by Lucas Bremgartner
  191.       if (game.done == IsFinishedWon)
  192.         game.done = HighScoreWon;
  193.       if (game.done == IsFinishedLost)
  194.         game.done = HighScoreLost;
  195. // Insert by Lucas Bremgartner (End)
  196.       score = 0;
  197.       insertPos = 0;
  198.       handled = true;
  199.     }
  200.     break;
  201.  
  202.   default:
  203.     break;
  204.   }
  205.   
  206.   CALLBACK_EPILOGUE
  207.   return handled;
  208. }
  209.